package com.pinc.poop;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class main extends PwdWidgetActivity {
	/** Called when the activity is first created. */
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initWidget();
		Intent i = new Intent(this, BroadcastService.class);
		startService(i);
	}

	@Override
	protected void initWidget() {
		super.initWidget();
		tv_left.setImageDrawable(getResources().getDrawable(R.drawable.setting));
		tv_left.setOnClickListener(new generalClickListener(setting.class));

		tv_right.setImageDrawable(getResources().getDrawable(R.drawable.exit));
		tv_right.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		tv_title.setText(getResources().getString(R.string.app_name));

		image_button b_cal = (image_button) this.findViewById(R.id.button_calendar);
		image_button b_st = (image_button) this.findViewById(R.id.button_statis);
		image_button b_abt = (image_button) this.findViewById(R.id.button_about);

		b_cal.setContent(R.drawable.calendar, getResources().getString(R.string.str_calendar)).setTextWidth(120);
		b_cal.setOnClickListener(new generalClickListener(calendar.class));
		b_st.setContent(R.drawable.statistics, getResources().getString(R.string.str_statistics)).setTextWidth(120);
		b_st.setOnClickListener(new generalClickListener(statistic.class));
		b_abt.setContent(R.drawable.about, getResources().getString(R.string.str_about)).setTextWidth(120);
		b_abt.setOnClickListener(new generalClickListener(about.class));

	}

	@Override
	protected void onResume() {
		super.onResume();

		new Thread(new Runnable(){
			@Override
			public void run() {
				Parser_Version pv = new Parser_Version();
				if (pv.parse())
				{
					try {
						if ( getPackageManager().getPackageInfo(getPackageName(), 0).versionCode < pv.getVersion())
							main.this.runOnUiThread(new Runnable(){
								@Override
								public void run() {
									showDialog(ID_CONFIRM);
								}
							});
					} catch (NameNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	private class generalClickListener implements View.OnClickListener
	{
		@SuppressWarnings("rawtypes")
		private Class which_class;

		@SuppressWarnings("rawtypes")
		public generalClickListener(Class c) {
			this.which_class = c;
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(main.this, which_class);
			startActivity(intent);
		}	
	}

	private static final int ID_CONFIRM = 1;
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch(id)
		{
		case ID_CONFIRM:
		{
			builder.setMessage(getResources().getString(R.string.str_update_notify))
			.setPositiveButton(getResources().getString(R.string.str_update), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Uri uri = Uri.parse("market://details?id=com.pinc.poop");
					Intent intent_market = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(intent_market);
				}
			})
			.setNegativeButton(getResources().getString(R.string.str_later), null);
			break;
		}
		default:
			break;
		}
		return builder.create();
	}
}